/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.dao.impl;

import java.util.List;
import mx.netbrain.dao.RepositoriesDao;
import mx.netbrain.model.Repositories;
import mx.netbrain.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author gamma
 */
public class RepositoriesDaoImpl implements RepositoriesDao{

    @Override
    public void save(Repositories repositories) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            session.beginTransaction();
            session.persist(repositories);
            session.beginTransaction().commit();
        }catch (Exception e){
            System.out.println("Error al guardar el registro del repositorio "+ e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void update(Repositories repositories) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            session.beginTransaction();
            session.merge(repositories);
            session.beginTransaction().commit();
        }catch (Exception e){
            System.out.println("Error al guardar el registro del repositorio "
                    + e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Repositories findById(Long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Repositories) session.load(Repositories.class, id);
    }

    @Override
    public List<Repositories> listAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from Repositories where name != 'Mis Documentos'").list();
    }

    @Override
    public void delete(Repositories repositories) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            session.beginTransaction();
            session.delete(repositories);
            session.beginTransaction().commit();
        }catch(Exception e){
            System.out.println("Error al eliminar el registro del repositorio");
            session.beginTransaction().commit();
        }
    }
    
}
